
package xj.graph2d.app; 

import java.util.*; 

import xj.graph2d.Document;
import xj.graph2d.Document.DocumentType;

public class DocumentRunnerManager { 

  public DocumentRunnerManager() {} 

  public void addCheckerRunner(DocumentType doctype, DocumentRunner runner) { 
    if (doctype != null && 
	runner != null) { 
      checkerRunnerMap.put(doctype, runner);
    }
  }

  public DocumentRunner findCheckerRunner(DocumentType doctype) { 
    if (doctype != null) { 
      return checkerRunnerMap.get(doctype);
    }
    return null;
  }

  public void startCheckerRunner(Document doc) { 
    if (checkerRunner != null) { 
      checkerRunner.stopThread(); 
    }

    if (doc != null) { 
      // run checker thread in the background 	
      checkerRunner = findCheckerRunner(doc.getDocumentType());
      if (checkerRunner != null) { 
	checkerRunner.startThread(3); // set thread priority to 3 
      }
    }
  }

  public void stopCheckerRunner() { 
    if (checkerRunner != null) { 
      checkerRunner.stopThread(); 
    }
  }

  public void startExecutionRunner(DocumentRunner runner) { 
    if (executionRunner != null) { 
      executionRunner.stopThread(); 
    }

    executionRunner = runner;
    if (executionRunner != null) { 
      executionRunner.startThread();
    }
  }

  public void stopExecutionRunner() { 
    if (executionRunner != null) { 
      executionRunner.stopThread();
    }
  }

  public void stopAllRunners() { 
    stopCheckerRunner();
    stopExecutionRunner();
  }

  protected Map<DocumentType, DocumentRunner> checkerRunnerMap = 
    new HashMap<DocumentType, DocumentRunner>();

  protected DocumentRunner executionRunner = null;
  protected DocumentRunner checkerRunner = null;

}